package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 15:19
 */
@CrossOrigin("*")
@RequestMapping("/category")
@RestController
public class CategoryContorller {
    @Autowired
    private DtsCategoryService dtsCategoryService;
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       Integer id, String name ,
                       @RequestParam(defaultValue = "add_time") String sort ,
                       @RequestParam(defaultValue = "desc") String order){
        IPage<DtsCategory> dtsCategoryIPage = dtsCategoryService.FindAll(page, limit, id, name, sort, order);
        List<DtsCategory> records = dtsCategoryIPage.getRecords();;
        long total = dtsCategoryIPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("items",records);
        return ResponseUtil.ok(map);
    }
    @RequestMapping("/l1")
    public Object l1(){
        List<CatVo> catVos = dtsCategoryService.finCatvo();
        return ResponseUtil.ok(catVos);
    }

    /**
     * 添加
     * @param dtsCategory
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.add(dtsCategory);
        return ResponseUtil.ok();
    }

    /**
     * 修改
     * @param dtsCategory
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.update(dtsCategory);
        return ResponseUtil.ok();
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.delete(dtsCategory);
        return ResponseUtil.ok();
    }


}
